﻿using MyHostSwtich.Core;
using MyHostSwtich.Datas.Models;

using System;
using System.IO;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;

namespace MyHostSwtich
{
    public partial class frmConfigItem : DockContent, IMdiItemForm
    {
        public frmConfigItem()
        {
            InitializeComponent();
        }

        public ConfigItem Context { get; set; }

        IMainForm IMdiForm.OwnerParent { get; set; }

        private void frmConfigItem_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        private void InitForm()
        {
            txtContent.SetHighlighting("INI");
            txtContent.LoadFile(this.Context.Filename);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            txtContent.SaveFile(this.Context.Filename);
        }

        private void btnUse_Click(object sender, EventArgs e)
        {
            try
            {
                var filename = Path.Combine(CommonRange.Instance.HostRootPath, "hosts");
                var content = this.txtContent.Text;
                File.WriteAllText(filename, content);
                MessageBox.Show($"你选择的配置方案已经启用.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(Exception ex)
            {
                MessageBox.Show($"在启用配置时发生错误.ex={ex.ToString()}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }

}
